import dayjs from 'dayjs'

/**
 * 获取数字类型的编号
 * @param step 增量
 * @param digit 位数
 */
export function getNumSerialNumber(step: number, digit: number) {
  // 起始值
  let start: number

  return {
    init: (preload: number) => {
      start = preload
    },
    get: () => {
      const res = start
      start += step
      // 是否为负数
      if (res < 0) {
        // 截掉负号位
        return `-${String(res).substring(1).padStart(digit, '0')}`
      } else {
        return String(res).padStart(digit, '0')
      }
    }
  }
}

/**
 * 获取字母类型的编号
 * @param start 起始
 * @param step 增量
 */
export function getCharSerialNumber(start: number, step: number) {
  // 生成函数
  function* generateFn() {
    const charSequence = 'abcdefghijklmnopqrstuvwxyz'
    // 固定索引
    let fixIndex = -1
    // 浮动索引
    let scrollIndex = -1

    while (true) {
      for (let i = 0; i < charSequence.length; i += step) {
        if (fixIndex > -1) {
          const mainCount = Math.floor(fixIndex / 26)
          const sub = fixIndex % 26

          // 几乎用不到
          let manStr = ''
          for (let i = 0; i < mainCount; i++) {
            manStr += charSequence
          }

          const subStr = charSequence.slice(0, sub + 1)

          const scrollStr = charSequence[scrollIndex]

          yield `${manStr}${subStr}${scrollStr}${charSequence[i]}`
        } else if (scrollIndex > -1) {
          const scrollStr = charSequence[scrollIndex]
          yield `${scrollStr}${charSequence[i]}`
        } else {
          yield `${charSequence[i]}`
        }

        if (i >= charSequence.length - step) {
          scrollIndex++

          if (scrollIndex > 25) {
            scrollIndex = 0
            fixIndex++
          }
        }
      }
    }
  }

  const generator = generateFn()

  for (let i = 0; i < start - 1; i++) {
    generator.next()
  }

  return {
    get: () => {
      return generator.next().value!
    }
  }
}

/**
 * 获取随机字符类型的编号
 * @param type 类型: 0 纯数字, 1 纯字母, 2 数字加字母
 * @param digit 位数
 */
export function getRandomSerialNumber(type: '0' | '1' | '2', digit: number) {
  let charSequence: string

  if (type === '0') charSequence = '1234567890'
  else if (type === '1') charSequence = 'abcdefghijklmnopqrstuvwxyz'
  else charSequence = '1234567890abcdefghijklmnopqrstuvwxyz'

  // 获取随机字符
  function getRandomChar() {
    return charSequence[Math.floor(Math.random() * charSequence.length)]
  }

  function* generateFn() {
    while (true) {
      let res = ''
      for (let i = 0; i < digit; i++) {
        res += getRandomChar()
      }

      yield res
    }
  }

  const generator = generateFn()

  return {
    get: () => {
      return generator.next().value!
    }
  }
}

/**
 * 获取时间类型的编号
 * @param type 时间类型: 0 当前时间, 1 文件创建时间, 2 文件修改时间
 * @param format 样式
 */
export function getDateSerialNumber(
  type: '0' | '1' | '2',
  format: '0' | '1' | '2' | '3' | '4' | '5' | '6'
) {
  // 解析模板
  let formatTempalte = ''

  if (format === '0') formatTempalte = 'YYYY-MM-DD'
  else if (format === '1') formatTempalte = 'YYYY年M月D日'
  else if (format === '2') formatTempalte = 'YYYYMMDD'
  else if (format === '3') formatTempalte = 'YYYY-MM-DD HH:mm:ss'
  else if (format === '4') formatTempalte = 'YYYY-MM-DD-HH-mm-ss'
  else if (format === '5') formatTempalte = 'YYYY年M月D日 HH时mm分ss秒'
  else if (format === '6') formatTempalte = 'YYYYMMDDHHmmss'

  if (type === '1') {
    return {
      get: (item: ITableDataItem) => {
        return dayjs(item.createTimeMs).format(formatTempalte)
      }
    }
  } else if (type === '2') {
    return {
      get: (item: ITableDataItem) => {
        return dayjs(item.updateTimeMs).format(formatTempalte)
      }
    }
  } else {
    return {
      get: () => {
        return dayjs().format(formatTempalte)
      }
    }
  }
}
